package com.gitee.zw.rule.utils;

import com.gitee.zw.rule.model.CompareOperator;

import java.util.Map;

public interface OperatorToExpression {

    static Map<String, OperatorToExpression> operatorToExpressionMap = Map.of(
            CompareOperator.CONTAINS_ANY.value(), new OperatorToExpression() {
                @Override
                public String toOpExpression(String operator, String param1, String param2) {
                    return "Functions.containsAny(" + param1 + ", " + param2 + ")";
                }
            },
            CompareOperator.CONTAINS_NONE.value(), new OperatorToExpression() {
                @Override
                public String toOpExpression(String operator, String param1, String param2) {
                    return "!Functions.containsAny(" + param1 + ", " + param2 + ")";
                }
            },
            CompareOperator.CONTAINS_ALL.value(), new OperatorToExpression() {
                @Override
                public String toOpExpression(String operator, String param1, String param2) {
                    return param2 + ".containsAll(" + param1 + ")";
                }
            },
//            CompareOperator.DECODE.value(), new OperatorToExpression() {
//                @Override
//                public String toOpExpression(String operator, String param1, String param2) {
//                    return "Functions.decode(" + param1 + ", " + param2 + ")";
//                }
//            },
            CompareOperator.LIKE.value(), new OperatorToExpression() {
                @Override
                public String toOpExpression(String operator, String param1, String param2) {
                    param2 = param2.replaceAll("%", "*").replaceAll("\\?", ".").replaceAll("\\*", ".*");
                    return param1 + ".matches(" + param2 + ")";
                }
            },
            CompareOperator.MATCHES.value(), new OperatorToExpression() {
                @Override
                public String toOpExpression(String operator, String param1, String param2) {
                    param1.matches(param2.replaceAll("%", "*").replaceAll("\\?", ".").replaceAll("\\*", ".*"));
                    return param1 + ".matches(" + param2 + ")";
                }
            },
            CompareOperator.IN.value(), new OperatorToExpression() {
                @Override
                public String toOpExpression(String operator, String param1, String param2) {
                    return param2 + ".contains(" + param1 + ")";
                }
            },
            CompareOperator.NOT_IN.value(), new OperatorToExpression() {
                @Override
                public String toOpExpression(String operator, String param1, String param2) {
                    return "!" + param2 + ".contains(" + param1 + ")";
                }
            }
    );

    public static String toExpression(String operator, String param1, String param2) {
        if (param2.trim().startsWith("decode(")) {
            param2 = param2.replaceAll("decode\\(", "Functions.decode(");
        }
        OperatorToExpression opExp = operatorToExpressionMap.get(operator);
        if (opExp == null) {
            return param1 + " " + operator + " " + param2;
        }
        return opExp.toOpExpression(operator, param1, param2);
    }

    String toOpExpression(String operator, String param1, String param2);

}
